package com.example.statistics.ui

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.example.statistics.R
import com.example.statistics.common.BaseActivity
import com.example.statistics.ui.main.FlowFragment
import com.example.statistics.ui.main.HomeFragment
import com.example.statistics.ui.main.MineFragment
import com.example.statistics.ui.main.PurchaseFragment
import com.google.android.material.bottomnavigation.BottomNavigationItemView
import com.google.android.material.bottomnavigation.BottomNavigationMenuView
import com.google.android.material.bottomnavigation.BottomNavigationView
import kotlinx.android.synthetic.main.activity_base.*
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.layout_top_bar.view.*


class MainActivity : BaseActivity() {
    override val title: String
        get() = "豫德源"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        top_bar.iv_top_bar_back.visibility = View.GONE
        //disableItemScale(nav_view)

        nav_view.setOnNavigationItemSelectedListener {
            view_pager.currentItem = it.order
            return@setOnNavigationItemSelectedListener true
        }
        view_pager.adapter = object : FragmentPagerAdapter(supportFragmentManager) {
            override fun getCount(): Int {
                return 4
            }

            override fun getItem(position: Int): Fragment {
                return when (position) {
                    0 -> HomeFragment()
                    1 -> FlowFragment()
                    2 -> PurchaseFragment()
                    3 -> MineFragment()
                    else -> HomeFragment()
                }
            }
        }
        view_pager.addOnPageChangeListener(object : OnPageChangeListener {
            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                nav_view.menu.getItem(position).isChecked = true
            }

            override fun onPageScrollStateChanged(state: Int) {
            }
        })
    }

    @SuppressLint("RestrictedApi")
    fun disableItemScale(view: BottomNavigationView) {
        try {
            val menuView = view.getChildAt(0) as BottomNavigationMenuView
            val mLargeLabelField = BottomNavigationItemView::class.java.getDeclaredField("mLargeLabel")
            val mSmallLabelField = BottomNavigationItemView::class.java.getDeclaredField("mSmallLabel")
            val mShiftAmountField = BottomNavigationItemView::class.java.getDeclaredField("mShiftAmount")
            val mScaleUpFactorField = BottomNavigationItemView::class.java.getDeclaredField("mScaleUpFactor")
            val mScaleDownFactorField = BottomNavigationItemView::class.java.getDeclaredField("mScaleDownFactor")
            mSmallLabelField.setAccessible(true)
            mLargeLabelField.setAccessible(true)
            mShiftAmountField.setAccessible(true)
            mScaleUpFactorField.setAccessible(true)
            mScaleDownFactorField.setAccessible(true)
            val fontScale = view.resources.displayMetrics.scaledDensity
            for (i in 0 until menuView.childCount) {
                val itemView = menuView.getChildAt(i) as BottomNavigationItemView
                val lagerObj = mLargeLabelField.get(itemView) as TextView
                val smallObj = mSmallLabelField.get(itemView) as TextView
                lagerObj.textSize = smallObj.textSize / fontScale + 0.5f
                mShiftAmountField.set(itemView, 0)
                mScaleUpFactorField.set(itemView, 1f)
                mScaleDownFactorField.set(itemView, 1f)
                itemView.setChecked(itemView.itemData!!.isChecked)
            }
        } catch (e: NoSuchFieldException) {
        } catch (e: IllegalAccessException) {
        }
    }

    override fun onBackPressed() {
        if (nav_view.menu.getItem(0).isChecked) {
            super.onBackPressed()
        } else {
            view_pager.currentItem = 0
        }
    }
}